<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          size="mini"
          @click="handleSearch"
          >搜索</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-check"
          size="mini"
          @click="check_check = true"
          :disabled="single"
          v-hasPermi="['purchase:pfmCheck:check']"
        >
          审核
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="single"
          @click="check_back = true"
          v-hasPermi="['purchase:pfmCheck:back']"
        >
          撤回
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['purchase:pfmCheck:export']"
        >
          导出
        </el-button>
      </el-col>

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      @scrollBottom="load"
      highlight-current-row
      border
      stripe
      height="calc(100% - 300px)"
      v-loading="loading"
      :data="pfmMstrList"
      @row-click="clickRow"
      :cell-style="iCellStyle"
      :header-cell-style="{ 'text-align': 'center' }"
    >
      <el-table-column label="#" width="40" align="center" />
      <el-table-column
        label="采购单号"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="pfmDjbh"
      />
      <el-table-column
        label="厂商"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="pfmCsdm"
      >
        <template #default="scope">
          <dict-tag :options="allPfmCsdmOptions" :value="scope.row.pfmCsdm" />
        </template>
      </el-table-column>
      <el-table-column
        label="采购预期"
        :show-overflow-tooltip="true"
        align="center"
        prop="pfmJhrq"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.pfmJhrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="备注"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="pfmDesc"
      />
      <el-table-column
        label="撤回原因"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="pfmNgdesc"
      />
      <el-table-column
        label="单据状态"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="pfmZtmc"
      />
      <el-table-column
        label="建立人员"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="pfmJlry"
      />
      <el-table-column
        label="建立日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="pfmJlrq"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.pfmJlrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="审核人员"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="pfmShry"
      />
      <el-table-column
        label="审核日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="pfmShrq"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.pfmShrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
    </el-table>
    <!--采购明细-->
    <el-tabs v-model="activeTab">
      <el-tab-pane label="采购明细" name="first">
        <el-table
          border
          stripe
          height="200px"
          :data="pfdDetList"
          :header-cell-style="{ 'text-align': 'center' }"
        >
          <el-table-column label="#" type="index" align="center" fixed="left" />
          <el-table-column label="请购单号" align="center" prop="pfdQfdh" />
          <el-table-column label="物料代码" align="center" prop="pfdWldm" />
          <el-table-column label="品名规格" align="center" prop="pfdPmgg" />
          <el-table-column
            label="单位"
            :show-overflow-tooltip="true"
            align="center"
            prop="pfdDw"
          >
            <template #default="scope">
              <dict-tag :options="unitFlagOptions" :value="scope.row.pfdDw" />
            </template>
          </el-table-column>
          <el-table-column label="采购数量" align="center" prop="pfdCgsl" />
          <el-table-column label="采购单价" align="center" prop="pfdCgdj" />
          <el-table-column label="采购金额" align="center" prop="pfdCgje" />
          <el-table-column label="备注" align="center" prop="pfdDesc" />
          <el-table-column label="变更原因" align="center" prop="pfdBgdesc" />
          <el-table-column label="申请部门" align="center" prop="pfdBmdm" />
          <el-table-column label="申请人员" align="center" prop="pfdQgry" />
          <el-table-column label="建立人员" align="center" prop="pfdJlry" />
          <el-table-column label="建立日期" align="center" prop="pfdJlrq" />
          <el-table-column label="变更人员" align="center" prop="pfdBgry" />
          <el-table-column label="变更日期" align="center" prop="pfdBgrq" />
          <el-table-column
            label="操作"
            align="center"
            fixed="right"
            width="100"
            class-name="small-padding fixed-width"
          >
            <template #default="scope">
              <el-button
                size="mini"
                type="text"
                @click="handleInvalid(scope.row)"
                :disabled="scope.row.pfdZtbz == '-2' || invalidSingle"
                >作废</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="采购统计" name="second">
        <el-table border stripe height="200px" :data="pfdDetTotal">
          <el-table-column label="#" type="index" align="center" fixed="left" />
          <el-table-column label="物料代码" align="center" prop="pfdWldm" />
          <el-table-column label="品名规格" align="center" prop="pfdPmgg" />
          <el-table-column
            label="单位"
            :show-overflow-tooltip="true"
            align="center"
            prop="pfdDw"
          >
            <template #default="scope">
              <dict-tag :options="unitFlagOptions" :value="scope.row.pfdDw" />
            </template>
          </el-table-column>
          <el-table-column label="采购数量" align="center" prop="pfdCgsl" />
          <el-table-column label="采购单价" align="center" prop="pfdCgdj" />
          <el-table-column label="采购金额" align="center" prop="pfdCgje" />
        </el-table>
      </el-tab-pane>
    </el-tabs>

    <!-- 查询对话框   -->
    <el-dialog
      :title="title"
      v-model="openSearch"
      :close-on-click-modal="false"
      draggable
      width="650px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-col :span="24">
          <el-form-item label="建立日期">
            <el-date-picker
              v-model="dateRange"
              size="small"
              style="width: 240px"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="采购单号" prop="pfmDjbh">
            <el-input
              v-model="queryParams.pfmDjbh"
              placeholder="请输入采购单号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="厂商" prop="pfmCsdm">
            <el-select
              v-model="queryParams.pfmCsdm"
              placeholder="请选择厂商"
              clearable
              size="small"
            >
              <el-option
                v-for="dict in pfmCsdmOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item label="状态标志" prop="pfmZtbz">
            <el-radio-group v-model="queryParams.pfmZtbz">
              <el-radio
                v-for="dict in ZtbzOptions"
                :key="dict.dictValue"
                :label="dict.dictValue"
                >{{ dict.dictLabel }}</el-radio
              >
            </el-radio-group>
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item
            style="margin-top: 25px; display: flex; justify-content: flex-end"
          >
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>

    <!-- 审核 -->
    <Modal v-model="check_check" draggable scrollable width="300">
      <p slot="header" style="color: #f60; text-align: center">
        <Icon type="ios-information-circle"></Icon>
        <span>确认审核？</span>
      </p>
      <div style="text-align: center">
        <p>审核后不可更改，请谨慎操作</p>
      </div>
      <div slot="footer">
        <Button type="success" size="large" long @click="handleCheck"
          >审核</Button
        >
      </div>
    </Modal>

    <!-- 撤回 -->
    <Modal v-model="check_back" draggable scrollable width="300">
      <p slot="header" style="color: #f60; text-align: center">
        <Icon type="ios-information-circle"></Icon>
        <span>确认撤回？</span>
      </p>
      <div style="text-align: center">
        <p>撤回理由</p>
        <Input v-model="reson" style="width: 75%" />
      </div>
      <div slot="footer">
        <Button type="error" size="large" long @click="handleBack">撤回</Button>
      </div>
    </Modal>
  </div>
</template>

<script>
import {
  listpfmMstr,
  listPfDdet,
  checkPfM,
  backPfm,
  invalidChild,
} from "@/api/purchase/pfmMstr";
import { getAllCs, getCs } from "@/api/putStorage/putStorageAdd";

export default {
  name: "PfmCheck",
  data() {
    return {
      //表格高度
      tableH: (document.body.clientHeight - 130) / 2,
      tableHChild: (document.body.clientHeight - 280) / 2,
      activeTab: "first",
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      pfmId: null,
      //选中数据
      clickData: {},
      // 非单个禁用
      single: true,
      //作废单个禁用
      invalidSingle: true,
      // 显示搜索条件
      showSearch: true,
      openSearch: false,
      // 总条数
      total: 0,
      // 零星采购单表格数据
      pfmMstrList: [],
      // 零星采购单明细表格数据
      pfdDetList: [],
      pfdDetTotal: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      check_check: false,
      check_back: false,
      // 撤回原因
      reson: null,
      //状态标志字典
      ZtbzOptions: [],
      // 厂商字典
      pfmCsdmOptions: [],
      // 全部厂商
      allPfmCsdmOptions: [],
      //单位
      unitFlagOptions: [],
      // 建立日期时间范围
      dateRange: [
        new Date(new Date().getTime() - 3600 * 1000 * 24 * 30),
        new Date(new Date().getTime() + 3600 * 1000 * 24 * 60),
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 25,
        pfmDjbh: null,
        pfmCsdm: null,
        pfmZtbz: "1",
        pfmTjflag: 1,
        pfmJlrq: null,
      },
      // 表单参数
      form: {},
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_purchase_state").then((response) => {
      this.ZtbzOptions = response.data;
    });
    getCs().then((response) => {
      this.pfmCsdmOptions = response.data;
      for (let item of this.pfmCsdmOptions) {
        item.dictLabel = item.csmCs;
        item.listClass = "default";
        item.dictValue = item.csmCsdm;
      }
    });

    getAllCs().then((response) => {
      this.allPfmCsdmOptions = response.data;
      for (let item of this.allPfmCsdmOptions) {
        item.dictLabel = item.csmCsmc;
        item.listClass = "default";
        item.dictValue = item.csmCsdm;
      }
    });

    //单位
    this.getDicts("sys_unit").then((response) => {
      this.unitFlagOptions = response.data;
    });
  },
  methods: {
    iCellStyle: function ({ row, column, rowIndex, columnIndex }) {
      //未审核
      console.log(row);
      if (row.pfmZtbz == "1" && row.pfmTjflag == 1) {
        return "padding:0px 0;color:green";
      }
      //已审核
      else if (row.pfmZtbz == "2" && row.pfmTjflag == 1) {
        return "padding:0px 0;color:#bb0331";
      }
    },
    /** 查询零星采购单列表 */
    getList(row) {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.dateRange && "" != this.dateRange) {
        this.queryParams.params["beginPfmJlrq"] = this.moment(
          this.dateRange[0]
        ).format("YYYY-MM-DD HH:mm:ss");
        this.queryParams.params["endPfmJlrq"] = this.moment(
          this.dateRange[1]
        ).format("YYYY-MM-DD HH:mm:ss");
      }
      this.queryParams.pageNum = 1;
      listpfmMstr(this.queryParams).then((response) => {
        this.pfmMstrList = [];
        this.pfdDetList = [];
        this.pfdDetTotal = [];
        this.pfmMstrList = this.pfmMstrList.concat(response.rows);
        this.total = response.total;
        this.loading = false;
        if (row != null) {
          this.clickRow(row);
        } else if (this.pfmMstrList != null && this.pfmMstrList.length > 0) {
          this.clickRow(this.pfmMstrList[0]);
        }
      });
    },
    //load
    load() {
      if (
        this.queryParams.pageNum <
        Math.ceil(this.total / this.queryParams.pageSize)
      ) {
        this.queryParams.pageSize = 25;
        this.queryParams.pageNum++;
        listpfmMstr(this.queryParams).then((response) => {
          this.pfmMstrList = this.pfmMstrList.concat(response.rows);
          this.total = response.total;
          this.loading = false;
        });
      }
      return 0;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openSearch = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        pfmId: null,
        pfmDjbh: null,
        pfmCsdm: null,
        pfmJhrq: new Date(),
        pfmZtbz: null,
        pfmTjflag: null,
        pfmDesc: null,
        pfmNgdesc: null,
        pfmJlry: null,
        pfmJlrq: null,
        pfmShry: null,
        pfmShrq: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.pfmMstrList = [];
      this.getList();
      this.openSearch = false;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    //选中单据
    clickRow(row) {
      this.clickData = row;
      this.single = !(
        row != null &&
        row.pfmZtbz == "1" &&
        row.pfmTjflag == "1"
      );
      this.invalidSingle = !(row != null && row.pfmZtbz == "2");
      this.pfmId = row.pfmId;
      this.getPfdList(row.pfmDjbh);
    },
    getPfdList(pfmDjbh) {
      listPfDdet(pfmDjbh).then((res) => {
        this.pfdDetList = res.data.detail;
        this.pfdDetTotal = res.data.total;
      });
    },
    handleSearch() {
      this.reset();
      this.title = "查询零星采购单";
      this.openSearch = true;
    },
    /** 审核 */
    handleCheck() {
      checkPfM(this.pfmId).then((res) => {
        if (res.code == "200") {
          this.check_check = false;
          this.getList();
          this.msgSuccess("审核成功");
        }
      });
    },
    /** 撤回 */
    handleBack() {
      backPfm(this.pfmId, this.reson).then((res) => {
        if (res.code == "200") {
          this.check_back = false;
          this.getList();
          this.msgSuccess("撤回成功");
        }
      });
    },
    /** 撤回按钮操作 */
    handleInvalid(row) {
      const pfdId = row.pfdId;
      this.$confirm("是否确认作废零星物料:" + row.pfdPmgg + "?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return invalidChild(pfdId);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("作废成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {},
  },
};
</script>

<style lang="scss" scoped></style>
